package ioc

import (
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"github.com/spf13/viper"
)

func InitNacosClient() naming_client.INamingClient {
	type NacosServerConfig struct {
		Ip   string `yaml:"ip"`
		Port uint64 `yaml:"port"`
	}
	var serverConfig NacosServerConfig
	err := viper.UnmarshalKey("grpc.nacos.server", &serverConfig)
	if err != nil {
		panic(err)
	}
	cli, err := clients.NewNamingClient(
		vo.NacosClientParam{
			ServerConfigs: []constant.ServerConfig{
				{
					IpAddr: serverConfig.Ip,
					Port:   serverConfig.Port,
				},
			},
		},
	)
	if err != nil {
		panic(err)
	}
	return cli
}
